package com.cmpsoft.mobile.plugin.pushnotification;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;

public class PushNotification extends CordovaPlugin {
    private BroadcastReceiver receiver = null;
    private CallbackContext pushCallbackContext = null;

    public static final String ACTION_RESPONSE = "bccsclient.action.RESPONSE";
    public static final String RESPONSE_METHOD = "method";
    public static final String RESPONSE_CONTENT = "content";
    public static final String RESPONSE_ERRCODE = "errcode";
    public static final String ACTION_BINDED = "com.baidu.android.pushservice.action.BINDED";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        if (action.equals("init")) {
            this.pushCallbackContext = callbackContext;
            super.initialize(cordova, webView);
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ACTION_BINDED);
            if (this.receiver == null) {
                this.receiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        try {
                            sendPushInfo(context, intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                cordova.getActivity().getApplicationContext().registerReceiver(this.receiver,
                        intentFilter);
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
            pluginResult.setKeepCallback(true);
            callbackContext.sendPluginResult(pluginResult);
            String apiKey = args.getString(0);
            PushManager.startWork(cordova.getActivity().getApplicationContext(), 0,
                    apiKey);
            return true;
        }
        return false;
    }

    private JSONObject getIds() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(cordova.getActivity());
        jsonObject.put("appId", sp.getString("appId", ""));
        jsonObject.put("channelId", sp.getString("channelId", ""));
        jsonObject.put("userId", sp.getString("userId", ""));
        return jsonObject;
    }

    private void sendPushInfo(Context context, Intent intent) throws JSONException {
        JSONObject info = getIds();
        info.put("deviceType", 3);
        if (this.pushCallbackContext != null) {
            PluginResult result = new PluginResult(PluginResult.Status.OK, info);
            result.setKeepCallback(false);
            this.pushCallbackContext.sendPluginResult(result);
        }
        if (this.receiver != null) {
            try {
                this.cordova.getActivity().unregisterReceiver(this.receiver);
                this.receiver = null;
            } catch (Exception e) {
            }
        }
    }
}
